<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<meta name="keywords" content="">
		<meta name="description" content="">
		<link rel="stylesheet" href="http://cmanage.shinechain.io/vendor/css/weuix.min.css" />
		<link rel="icon" type="image/png" href="http://cmanage.shinechain.io/imgs/favicon.ico" />
		<title>区块链推广管理中心</title>
	</head>
	<style>
		body{
			background: #ffffff;
		}
		.main{
			width: 80%;
			margin: 0 auto;
		}
		.main>.title{
			text-align: center;
			margin-top: 5%;
		}
		#template{
			display: none;
		}
	</style>

	<body>
		<div class='main' style="position: relative;">
			<a href="/telegram.html" style="position: absolute; top: -10px;"><img src="http://cmanage.shinechain.io/imgs/return.jpg" height="48" width="48" style="vertical-align:middle;" /></a>
			<p>
				<h2 class='title'>邀请用户入群送Token<h2></p>
	
	<div>
       <br />
       	
    </div>
    
    <h3>规则：</h3>
                每拉一个新用户入群，获得<input type="text" id='token' size="1"/>，
                每天上限是<input type="text" id='tokenDayTimes' size="1"/>个用户（输入-1则无限制）
                <input type="button" id="formSubmitBtn" value="保存"/>
       			<div>
       				<br />
       			</div>
   
 <div id='template' class='block'>
		<div class='ctrl'>
			{{index}}{{name}}
			<input class="weui_switch" type="checkbox" {{checked}} data-slug='{{slug}}'>
		</div>
	</div>
	
</div>
<table id="table" border="1" cellspacing="0" cellpadding="0" align="center" style="font-size: large;left: 300px;position: relative;">
	<tbody>
	   <tr>
	        <th>电报Id</th>
	        <th>电报昵称</th>
	        <th>拉人总数</th>
	        <th>已获Token</th>
	    </tr>
    </tbody>
</table>
		<script src='http://cmanage.shinechain.io/vendor/js/zepto.min.js'></script>
<script>
$(function(){
	var ip = 'http://bot.shinechain.io';
	var options = [{
		'name':'恭喜*用户*，成功拉取*被拉用户*进群,共获得*个*共识token',
		'checked': '',
		'slug': 'useTokenSay'
		}];
	var setButton = function() {
		$('.block').each(function() {
		if(!$(this).find('.title input').is(":checked")) {
			$(this).find('.body a').hide();
			$(this).find('.body p').show();
		} else {
			$(this).find('.body a').show();
			$(this).find('.body p').hide();
		}
		if($(this).find('.body a').attr('href') == '') {
			$(this).find('.body a').hide();
			$(this).find('.body p').show();
		}
		})
	}			
	
 	var getInfo = function () {
			$.get(ip + '/botApi/getSendToken', function(res) {
				options.map(function(value, index) {
							var slug = value.slug;
							if(res.useToken== true) {
								options[index]['checked'] = 'checked';
							}
					});

				options.map(function(value, index){
					var html = $('#template').clone().attr('id', '').prop("outerHTML");
					html = html.replace("{{index}}", '获奖提示：');
					for(var index in value){
						html = html.replace("{{"+index+"}}", value[index]);
					}
					$('.main').append(html)
				});
				setButton();
				clickThing();
		        
		        //input
		        $('#token').val(res.token);
		        $('#tokenDayTimes').val(res.tokenDayTimes);
		        
		        //table
		        var table=document.getElementById("table");
				var data = res.inviterRewardCount;
				$.each(data,function(key,values){
					var row=table.insertRow(table.rows.length);
					var c1=row.insertCell(0);
					c1.innerHTML=values.telegrameId;
					var c2=row.insertCell(1);
					c2.innerHTML=values.telegramName;
					var c1=row.insertCell(2);
					c1.innerHTML=values.inviteCount;
					var c2=row.insertCell(3);
					c2.innerHTML=values.tokenCount;
    				});     
 			});  
			
	}
			getInfo();		
  	var clickThing = function (){
		$('.weui_switch').click(function() {
			var slug = $(this).data('slug');
			var checked = $(this).is(":checked");
			var self = $(this);
			$.get(ip+'/botApi/botMethod?botMethod='+slug, function(result){
				if (result && result.status == 200){
					if(result.botMethod == true){
						self.prop("checked",true); 
					}else{
						self.prop("checked",false); 
					}	
				setButton();
					}else {
						indow.alert(result.errorMsg);
					}
			});								
		});			
	}
  	var clickToken = function (){
  		$('#formSubmitBtn').click(function(){
		var token = $('#token').val();
		var tokenDayTimes = $('#tokenDayTimes').val();
		var jsonArr = {token,tokenDayTimes};
		$.ajax({
                url: ip + '/botApi/token',
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                data: JSON.stringify(jsonArr),
                success: function(res) {
                	if(res.status==200){
                	$('#token').text(res.token);
                	$('#tokenDayTimes').text(res.tokenDayTimes);
                	window.alert('保存成功');
                	}
                	else{
                		window.alert(res.errorMsg);
                	}
                },
      		 })
  		});
  	}
  clickToken();
  clickThing();
})
</script>
</body>
</html>